//
//  CategoryManager.swift
//  SwiftToDoList
//
//  Created by 谢志敏 on 2025/7/8.
//

import Foundation
import CoreData

class CategoryManager {
    
    static func createCategory(title: String, name: String, color: String) -> Category {
        let context = CoreDataManager.shared.context
        let newCategory = Category(context: context)
        newCategory.id = UUID()
        newCategory.name = name
        newCategory.color = color
        
        return newCategory
    }
    
    static func fetchAllCategories() -> [Category] {
        let context = CoreDataManager.shared.context
        let request: NSFetchRequest<Category> = Category.fetchRequest()
        
        do {
            let categories = try context.fetch(request)
            return categories
        } catch {
            print("获取Category失败 \(error)")
            return []
        }
    }
    
    static func deleteCategory(category: Category) {
        let context = CoreDataManager.shared.context
        context.delete(category)
        CoreDataManager.shared.saveContext()
    }
}
